package com.weirong.smartvault.backservice.service;

import com.weirong.smartvault.dao.CommandRepository;
import com.weirong.smartvault.entity.Command;
import com.weirong.smartvault.entity.CommandResponse;
import com.weirong.smartvault.entity.DeliveryLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: kfeb4
 * Date: 2017/12/30
 * Time: 12:52
 */
@Service
public class CommandService {
    @Autowired
    CommandRepository commandRepository;

    public Page<Command> findByCreatedTimeBetweenWithPaging(Date start, Date end, Integer page, Integer count) {
            Specification<Command> specification = (root, criteriaQuery, criteriaBuilder) -> {
                Path<Date> time = root.get("createdTime");
                Predicate predicate = criteriaBuilder.between(time,start,end);
                return criteriaBuilder.and(predicate);
            };
            Sort sort = new Sort(Sort.Direction.DESC, "id");
            Pageable pageable = new PageRequest(page, count, sort);
            return commandRepository.findAll(specification,pageable);
    }
}
